package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BBeginnerArticleTypeEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BPlatformBeginnerArticleTypeEntity;
import io.lkt.modules.sys.service.BPlatformBeginnerArticleTypeService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 新手学堂文章类别所属公众号
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-12-15 15:16:02
 */
@RestController
@RequestMapping("/sys/bplatformbeginnerarticletype")
public class BPlatformBeginnerArticleTypeController {
    @Autowired
    private BPlatformBeginnerArticleTypeService bPlatformBeginnerArticleTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bplatformbeginnerarticletype:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bPlatformBeginnerArticleTypeService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/queryList")
//    @RequiresPermissions("sys:bbeginnerarticletype:list")
    public R queryList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);

        List<BPlatformBeginnerArticleTypeEntity> list = bPlatformBeginnerArticleTypeService.queryList(query);
        int total = bPlatformBeginnerArticleTypeService.queryListTotal(query);

        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{articleTypeId}")
//    @RequiresPermissions("sys:bplatformbeginnerarticletype:info")
    public R info(@PathVariable("articleTypeId") Integer articleTypeId){
        BPlatformBeginnerArticleTypeEntity bPlatformBeginnerArticleType = bPlatformBeginnerArticleTypeService.getById(articleTypeId);

        return R.ok().put("bPlatformBeginnerArticleType", bPlatformBeginnerArticleType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bplatformbeginnerarticletype:save")
    public R save(@RequestBody BPlatformBeginnerArticleTypeEntity bPlatformBeginnerArticleType){
        bPlatformBeginnerArticleTypeService.save(bPlatformBeginnerArticleType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bplatformbeginnerarticletype:update")
    public R update(@RequestBody BPlatformBeginnerArticleTypeEntity bPlatformBeginnerArticleType){
        ValidatorUtils.validateEntity(bPlatformBeginnerArticleType);
        bPlatformBeginnerArticleType.setModifyTime(DateUtil.getCurDate());
        bPlatformBeginnerArticleTypeService.updateByInfo(bPlatformBeginnerArticleType);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bplatformbeginnerarticletype:delete")
    public R delete(@RequestBody Integer[] articleTypeIds){
        bPlatformBeginnerArticleTypeService.removeByIds(Arrays.asList(articleTypeIds));

        return R.ok();
    }

    @RequestMapping("/getInfo")
    public R getInfo(@RequestParam String gzhCode, @RequestParam Integer articleTypeId) {
        if (StringUtils.isBlank(gzhCode) || Objects.isNull(articleTypeId) || articleTypeId.intValue() <= 0) {
            return R.error("参数异常");
        }

        BPlatformBeginnerArticleTypeEntity bPlatformBeginnerArticleType = bPlatformBeginnerArticleTypeService.getInfo(gzhCode, articleTypeId);

        return R.ok().put("bPlatformBeginnerArticleType", bPlatformBeginnerArticleType);
    }

    @RequestMapping("/getArticleTypeByGzhCode/{gzhCode}")
    public R getArticleTypeByGzhCode(@PathVariable("gzhCode") String gzhCode) {
        if (StringUtils.isBlank(gzhCode)) {
            return R.error("参数异常");
        }

        List<BBeginnerArticleTypeEntity> articleTypeIdList = bPlatformBeginnerArticleTypeService.getArticleTypeByGzhCode(gzhCode);

        return R.ok().put("articleTypeIdList", articleTypeIdList);
    }

}
